﻿Imports FuncClasses
Imports Biller

Public Class RibbonTab
    Dim vm As DeliveryOrdersViewModel
    Private _order As Order
    Private _db As IDatabase
    Private _user As User
    Private _srcControl As Biller.AuftragControl
    Private _parent As CustomPlugIn

    Public Sub New(parent As CustomPlugIn, ByRef order As Order, ByRef user As User, ByRef srcControl As Biller.AuftragControl)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        _parent = parent
        _order = order
        _db = parent.Database
        _user = user
        _srcControl = srcControl
        '_parent = parent
        vm = New DeliveryOrdersViewModel(Nothing, Dispatcher, _db, user)
        ListView4.DataContext = vm
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        For Each o As OrderPreviewModel In ListView4.SelectedItems
            Dim order = _db.GetOrder(o.OrderID, o.OrderTyp, _user)
            For Each a As OrderedArticle In order.OrderedArticles
                If String.IsNullOrEmpty(a.OrderText) Then
                    a.OrderText = "Lieferschein Nr. " + o.OrderID.ToString
                Else
                    a.OrderText += vbCrLf + "Lieferschein Nr. " + o.OrderID.ToString
                End If
                _order.OrderedArticles.Add(a)
            Next
        Next
        _parent.SetViewToArticle(_srcControl)
    End Sub

    Private Sub TabItem1_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles TabItem1.Loaded
        'Refresh Customer
        vm.Customer = _order.Customer
        If vm.Customer IsNot Nothing AndAlso Customer.IsEmpty(vm.Customer) = False Then
            TabControl.SelectedIndex = 1
        Else
            TabControl.SelectedIndex = 0
        End If
        ListView4.ItemsSource = vm.DeliveryOrders
    End Sub
End Class
